package com.bolun998300.personal_blog_springboot.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import com.bolun998300.personal_blog_springboot.dao.BbsDao;
import com.bolun998300.personal_blog_springboot.pojo.Bbs;
import com.bolun998300.personal_blog_springboot.pojo.Comment;

@Service
public class BbsService {
	@Autowired
	private BbsDao bbsDao;

	public Page<Bbs> listByPage(int page, int size, String operate) {
		Sort sort = new Sort(Sort.Direction.DESC,"bbs_date");
        Pageable pageable = new PageRequest(page-1,size);
        Page<Bbs> pageFromJpa = null;
        if("all".equals(operate)){
            pageFromJpa = bbsDao.findAllByOrderByBbsDateDesc(pageable);
        }else if("notReview".equals(operate)){
            pageFromJpa = bbsDao.findByBbsStateOrderByBbsDateDesc(0,pageable);
        }else if("reviewed".equals(operate)) {
        	pageFromJpa = bbsDao.findByBbsStateOrderByBbsDateDesc(1,pageable);
        }
        return pageFromJpa;
		
	}
	
	public Bbs get(int id) {
		return bbsDao.findOne(id);
	}

	public void update(Bbs bbs) {
		bbsDao.save(bbs);
	}


    public void save(Bbs bbs) {
		bbsDao.save(bbs);
    }
}
